<?php

/***

LZRoster: A roster for World of Warcraft.
Copyright (C) 2007  Emmanuel Cron

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

***/

require_once dirname(__FILE__)."/Log.class.php";
require_once dirname(__FILE__)."/Config.class.php";

class Sql
{
	static private $instance;
	
	private $config;
	
	private $queries = 0;
	private $num_results = 0;
	private $results = null;
	
	private $log;
	
	private function __construct()
	{
		$this->log = new Log("sql");
		$this->config = Config::GetInstance();
		
		if (! @mysql_connect($this->config->GetSqlServer(), $this->config->GetSqlUser(), $this->config->GetSqlPassword()))
		{
			trigger_error("Unable to connect to database.", E_USER_ERROR);
		}
		
		if (! mysql_select_db($this->config->GetSqlDatabase()))
		{
			@mysql_close();
			
			trigger_error("Unable to select database.", E_USER_ERROR);
		}
	}
	
	static function GetInstance()
	{
		if ( empty(self::$instance) )
		{
			self::$instance = new Sql();
		}
		
		return self::$instance;
	}
	
	static function HasInstance()
	{
		if ( empty(self::$instance) )
		{
			return false;
		}
		else
		{
			return true;
		}
	}
	
	static function Test($server, $database, $user, $password)
	{
		if (! @mysql_connect($server, $user, $password))
		{
			return false;
		}
		
		if (! mysql_select_db($database))
		{
			@mysql_close();
			
			return false;
		}
		
		mysql_close();
		
		return true;
	}
	
	function Query($q)
	{
		$return_value = mysql_query($q);
		$this->queries++;
		
		$this->results = $return_value;
		
		if (is_resource($this->results))
		{
			$this->num_results += mysql_num_rows($this->results);
			
			return true;
		}
		else
		{
			if (strlen(mysql_error()) > 0)
			{
				$this->log->AddError("mysql_query() produced an error: ".mysql_error()."\n----------QUERY WAS\n".$q."\n----------END OF QUERY--");
			}
			
			return $return_value;
		}
	}
	
	function IterateResults()
	{
		return @mysql_fetch_assoc($this->results);
	}
	
	function GetLastError()
	{
		return mysql_error();
	}
	
	function GetResultsNumber()
	{
		if ( mysql_affected_rows() > 0 )
		{
			return mysql_affected_rows();
		}
		elseif ( is_resource($this->results) )
		{
			return mysql_num_rows($this->results);
		}
		else
		{
			return 0;
		}
	}
	
	function GetEntriesNumber()
	{
		return $this->num_results;
	}
	
	function GetQueriesNumber()
	{
		return $this->queries;
	}
	
	function __destruct()
	{
		@mysql_close();
	}
}

?>